﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;

public class ResourceHelper {
    public static readonly string originalHotFixPath = Application.dataPath + "/Resources/" + ResourcePath.HotFixFolderName;
    public static readonly string bundleConfigPath = Application.dataPath + "/Resources/" + ResourcePath.HotFixFolderName + "/BundleConfig/BundleConfig.txt";
    public static readonly string guiDependencyPath = Application.dataPath + "/Resources/" + ResourcePath.HotFixFolderName + "/GUIDependency/GUIdependency.txt";
    public static readonly string bundleConfigPathAddAssets = "Assets/Resources/" + ResourcePath.HotFixFolderName + "/BundleConfig/BundleConfig.txt";
    public static readonly string bundleConfigFolderName = "BundleConfig";
    public static readonly string bundleConfigFileName = "BundleConfig.txt";
    public static readonly string guiDependencyFolderName = "GUIDependency";
    public static readonly string guiDependencyFileName = "GUIdependency.txt";
    public static readonly string targetHotFixMovePath = Application.dataPath + "/../../";
    public static readonly string luaFolderName = "Lua";
    public static readonly string win64Platform = "Win64";
    public static readonly string osxPlatform = "OSX";

    public static void CopyLua(string srcPath, string targetPath, Dictionary<int, string> ignoreFolderDic, string extension = null)
    {
        try
        {
            bool needAddExtension = !string.IsNullOrEmpty(extension);

            if (targetPath[targetPath.Length - 1] != Path.DirectorySeparatorChar)
            {
                targetPath += Path.DirectorySeparatorChar;
            }

            if (!Directory.Exists(targetPath))
            {
                Directory.CreateDirectory(targetPath);
            }

            string[] fileList = Directory.GetFileSystemEntries(srcPath);

            foreach (string file in fileList)
            {
                string targetFileName = targetPath + Path.GetFileName(file);

                FileAttributes attr = File.GetAttributes(file);
                if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
                {
                    string shortName = Path.GetFileName(file);
                    if (ignoreFolderDic != null && ignoreFolderDic.ContainsValue(shortName)) continue;

                    CopyLua(file, targetFileName, ignoreFolderDic, extension);
                }
                // process File
                else
                {
                    if (targetFileName.EndsWith(".meta"))
                        continue;

                    if (needAddExtension)
                        targetFileName = targetFileName + extension;

                    File.Copy(file, targetFileName, true);
                }
            }
        }
        catch (Exception e)
        {
            throw;
        }
    }

    public static void CopyGenerateBundleConfig(string destionateFolder)
    {
        try
        {
            string targetFile = destionateFolder + "/" + bundleConfigFolderName + "/" + bundleConfigFileName;
            FileInfo fi = new FileInfo(targetFile);
            fi.Directory.Create();
            File.Copy(bundleConfigPath, targetFile);
        }
        catch(Exception e)
        {
            Debug.LogError("拷贝BundleConfig文件出错，具体信息: " + e.ToString());
        }
    }

    public static void CopyGUIDependencyFile(string destionateFolder)
    {
        try
        {
            string targetFile = destionateFolder + "/" + guiDependencyFolderName + "/" + guiDependencyFileName;
            FileInfo fi = new FileInfo(targetFile);
            fi.Directory.Create();
            File.Copy(guiDependencyPath, targetFile);
        }
        catch (Exception e)
        {
            Debug.LogError("拷贝BundleConfig文件出错，具体信息: " + e.ToString());
        }
    }
}
